package com.zx.platform.utils;

import com.zx.map.utils.MyBeanUtils;
import sun.misc.BASE64Decoder;

import java.io.*;
import java.util.zip.GZIPInputStream;

/**
 * 序列化工具类
 *
 * @author jianxue.zhang
 * @version $Id: ReportObjectSerializerUtil.java, v 0.1 2013-9-12 下午2:34:17 jianxue.zhang Exp $
 */
@SuppressWarnings("restriction")
public class AccountObjectSerializerUtil {

    /**
     * 序列化压缩、加密对象
     *
     * @param obj
     * @return
     * @throws IOException
     */
    public static String serialize(Object obj) throws IOException {
        return MyBeanUtils.beanToJson(obj);
        /*ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        GZIPOutputStream gzip = new GZIPOutputStream(bos);

        try {
            oos.writeObject(obj);
            oos.flush();
            byte abyte0[] = baos.toByteArray();
            gzip.write(abyte0);
            gzip.flush();
            gzip.finish();
            bos.flush();
            byte[] bytes = bos.toByteArray();
            return (new Base64Encode()).encode(bytes);
        } finally {
            if (oos != null) {
                oos.close();
            }
            if (baos != null) {
                baos.close();
            }
            if (gzip != null) {
                gzip.close();
            }
            if (bos != null) {
                bos.close();
            }
        }*/
    }

    /**
     * 字符串转为对象
     *
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    @SuppressWarnings("restriction")
    public static Object unserialize(String jsonString) throws IOException, ClassNotFoundException {
        return MyBeanUtils.jsonToBean(jsonString,Object.class);
        /*byte[] abyte1 = new BASE64Decoder().decodeBuffer(objectString);
        ByteArrayInputStream bais = new ByteArrayInputStream(abyte1);
        GZIPInputStream gzip = new GZIPInputStream(bais);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int num = -1;
        while ((num = gzip.read(buf, 0, buf.length)) != -1) {
            baos.write(buf, 0, num);
        }
        baos.flush();
        ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        try {
            Object obj = ois.readObject();
            return obj;
        } finally {
            if (ois != null) {
                ois.close();
            }
            if (bis != null) {
                bis.close();
            }
            if (baos != null) {
                baos.close();
            }
            if (gzip != null) {
                gzip.close();
            }
            if (bais != null) {
                bais.close();
            }
        }*/
    }

    /**
     * 字符串转为对象
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    @SuppressWarnings("restriction")
    public static Object unserialize2(String jsonString) throws IOException, ClassNotFoundException {
        byte[] abyte1 = new BASE64Decoder().decodeBuffer(jsonString);
        ByteArrayInputStream bais = new ByteArrayInputStream(abyte1);
        GZIPInputStream gzip = new GZIPInputStream(bais);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int num = -1;
        while ((num = gzip.read(buf, 0, buf.length)) != -1) {
            baos.write(buf, 0, num);
        }
        baos.flush();
        ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        try {
            Object obj = ois.readObject();
            return obj;
        } finally {
            if (ois != null) {
                ois.close();
            }
            if (bis != null) {
                bis.close();
            }
            if (baos != null) {
                baos.close();
            }
            if (gzip != null) {
                gzip.close();
            }
            if (bais != null) {
                bais.close();
            }
        }
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Object ob = AccountObjectSerializerUtil.unserialize("H4sIAAAAAAAAAI1VPWwcRRQen3/i88V/caSAUAQN5i/c7e7d+m4VRWBsiOycE8s2RaCa2x2f196fYWbO3nOBSBOE0kQICoRcQO0OJCoaGuhApoQChCIUCSooiGh4s7e7t7d3Jl7JP/O9N2/evPd9b47/RKOcoZdN3y0eBkUX0yInbN82SdH1LeIU1wgzd7AnVrxtf8u3/C3M9/5wXvnq0bVnf8ih0Tq65EYeS4QJe9s2sSDSWaBiHaKWDoMSRC1Z2C+FEUtrg/2v1tGsmzpsTToLdCUdJEotEyjxhhBznAjhkCXMrFSQ4ulBNvv9ZSaCMNf2sBMCdZsLgWbqu3gfl1rCdkoSAbdJ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